/*
 * InputProviderCondition.java
 *
 * Stateroom - Java framework library for finite state machines
 * Copyright (C) 2009 by Matthew Werny
 * All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.iceflow.stateroom.conditions;

import net.iceflow.stateroom.Context;
import net.iceflow.stateroom.interfaces.Condition;
import net.iceflow.stateroom.interfaces.InputProvider;
import net.iceflow.stateroom.Utility;
import net.iceflow.stateroom.models.InputProviderModel;

/**
 * Condition that evaluates the truth of the next object from the input provider matching the match reference object.
 * @param <T> Type used to represent the input alphabet for the input provider
 */
public class InputProviderCondition<T> implements Condition<InputProviderModel<T>> {

   /** Reference object used to determine truth of the current object from the input provider being a match for this instance. */
   private final T _match;

   /**
    * Constructs a new InputProviderCondition containing the provided match reference object.
    * @param match Reference object used for comparison against input objects from the input provider
    */
   public InputProviderCondition(T match) {
      // Validate the argument to make sure that it is not null.
      Utility.validateNotNull(match, "match may not be null.");

      // Define the finalized match based on the parameter
      this._match = match;
   }

   /**
    * Getter for the match field.
    * @return the match
    */
   public T getMatch() {
      // Return the reference object instance used to determine matches
      return this._match;
   }

   /**
    * Indicates whether the current object available from the input provider matches the reference object.
    * @return truth of whether a match condition exists
    */
   @Override
   public boolean isMet(Context<InputProviderModel<T>> context) {
      // Assume that there was no match, until it can be determined otherwise
      boolean truthOfMatch = false;

      // Extract the inner input provider from the context
      InputProvider<T> provider = context.getModel().getProvider();

      // Extract the next available value from the input provider and leave it in place, in case no match is found.
      T nextValue = provider.peekNext();

      // If the extracted next value matches the reference object...
      if (this._match.equals(nextValue)) {
         // Remove the next value from the input provider
         provider.getNext();

         // Indicate that there indeed was a match that was found
         truthOfMatch = true;
      }

      // Return the determination that was made
      return truthOfMatch;
   }
}
